<?php

/**
 * @author: zhangxingning
 * @contact: zhixiao476@gmail.com
 * @file: ZhixiaoAuthenticate.php
 * @time: 2021/4/30 11:43
 * @desc:
 */


namespace App\Http\Middleware;
use App\Libraries\Utility;
use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AsAuthenticate {

    /**
     * 权限验证
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next, ...$guards){
        if (!Auth::guard($guards)->check()) {

            if (Utility::isAjaxRequest()) {
                $msg = ['code' => 0, 'msg' => '抱歉，您还没有登录获取访问权限！', 'url' => url('login')];
                throw new AuthenticationException(json_encode($msg, JSON_UNESCAPED_UNICODE), $guards);
            } else {
                return redirect('login');
            }
        }

        return $next($request);
    }
}
